package com.d.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.d.business.domain.dto.BAnnexDTO;
import com.d.business.domain.dto.ClauseDataDTO;
import com.d.client.dto.BPurchaseOrderBrandDTO;

import java.util.List;
import java.util.Map;

public class YidaFileAddressToJson {


    /**
     * @param files
     * @param fileType 1图片2其他文件
     * @return
     */
    public static String getFileStr(List<BAnnexDTO> files, Integer fileType) {
        if (files != null) {
            String arr = "[";
            boolean first = true;
            for (BAnnexDTO file : files) {
                String suffix = file.getUrl().split("\\.")[file.getUrl().split("\\.").length - 1];
                if (fileType == 1) {
                    if (!suffix.equals("png") && !suffix.equals("jpg") && !suffix.equals("bmp") && !suffix.equals("jpeg")) {
                        continue;
                    }
                } else {
                    if (suffix.equals("png") || suffix.equals("jpg") || suffix.equals("bmp") || suffix.equals("jpeg")) {
                        continue;
                    }
                }
                JSONObject f3 = new JSONObject();
                f3.put("downloadUrl", file.getUrl());
                if (file.getName() != null) {
                    if(file.getName().split("\\.").length>1){
                        f3.put("name", file.getName());
                    }else{
                        f3.put("name", file.getName()+"."+suffix);
                    }
//                    f3.put("name", file.getName().split("\\.")[0]);
                }
                f3.put("previewUrl", file.getUrl());
                f3.put("url", file.getUrl());

                if (fileType == 1) {
                    f3.put("size", 10000);
                } else {
                    f3.put("ext", suffix);
                }

                if (!first) {
                    arr += ",";
                }
                arr += f3.toString();
                first = false;
            }
            arr += "]";
            return arr;
        }
        return null;
    }

    /**
     * @param purchaseOrderBrandDTOS
     * @param item                   带入宜搭定义字段
     * @return
     */
    public static String getPurchaseOrderBrandListStr(List<BPurchaseOrderBrandDTO> purchaseOrderBrandDTOS, Map<String, String> item) {
        if (purchaseOrderBrandDTOS != null) {
            String arr = "[";
            boolean first = true;
            for (BPurchaseOrderBrandDTO brandDTO : purchaseOrderBrandDTOS) {
                JSONObject f3 = new JSONObject();
                f3.put(item.get("品牌汇总的品牌名称"), brandDTO.getBrandName());
                f3.put(item.get("品牌汇总的采购金额"), brandDTO.getPurchasePrice());
                f3.put(item.get("品牌汇总的采购数量"), brandDTO.getPurchaseNum());
                if (!first) {
                    arr += ",";
                }
                arr += f3.toString();
                first = false;
            }

            arr += "]";
            return arr;
        }
        return null;
    }

    public static String getContractListStr(List<ClauseDataDTO> clauseDataDTOS, Map<String, String> item) {
        if (clauseDataDTOS != null) {
            String arr = "[";
            boolean first = true;
            for (ClauseDataDTO brandDTO : clauseDataDTOS) {
                JSONObject f3 = new JSONObject();
                f3.put(item.get("违约条款的条款隶属"), brandDTO.getParty());
                f3.put(item.get("违约条款的条款类型"), brandDTO.getType());
                f3.put(item.get("违约条款的条款说明"), brandDTO.getText());
                f3.put(item.get("违约条款的违约金额"), brandDTO.getAmt());
                f3.put(item.get("违约条款的违约比率"), brandDTO.getRate());
                if (!first) {
                    arr += ",";
                }
                arr += f3.toString();
                first = false;
            }

            arr += "]";
            return arr;
        }
        return null;
    }
}
